/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.ucm;

import assignment_cp3120.DAO.Factory.DAOFactory;
import assignment_cp3120.administrator.createmanager.RegisterManagerModel;
import assignment_cp3120.administrator.createmanager.RegisterManagerView;
import assignment_cp3120.dataObjects.User;
import assignment_cp3120.startup.StartupView;

/**
 * 
 *  REGISTER USER USE-CASE
 * 
 * Precondition: register manager view is shown
 * 
 * 1. User fills out the form and presses submit
 * 2. System validates the data
 * 3. System registers the new user
 * 4. System shows login view
 * <P>
 * 2.a.1 System detects that forms are not full
 * 2.a.2 System displays error message
 * <P>
 * 2.b.1 System detects that the username exists in database
 * .b.2 System displays error
 * <P>
 * 2.c.1 System detects that the password and confirm fields are the same
 * 2.c.2 System gives error and clears fields
 * 
 * Postcondition: Display is updated accordingly 
 * 
 * @author Ricbard Irons
 */
public class UCRegisterManager implements UseCaseController{
    private RegisterManagerView view;
    private RegisterManagerModel model;

    public UCRegisterManager() {
        this(new RegisterManagerView(), new RegisterManagerModel());
    }
    
    public UCRegisterManager(RegisterManagerView view, RegisterManagerModel model) {
        this.view = view;
        this.model = model;
    }

    public void run() {
        if (view.validateForm()){
            setModelToFormData();
            if (model.isUniqueUsername()){
                if (model.isPassConfirmEqual()){
                    DAOFactory.getDAOFactory().getUserDAO().addUser(model.getUser());
                    view.hideGUI();
                }
                else {
                    StartupView.getInstance().errorMessageBox("Password and Comfim must be the same", "Error!");
                }
            } else {
                StartupView.getInstance().errorMessageBox("Username already exists please choose a new one", "Error!");
            }
        }
        else {
            StartupView.getInstance().errorMessageBox("Some inputs are not valid", "Not Valid!");
        }
    }
    /**
     * Takese data from the from and sets it to the model
     */
    private void setModelToFormData(){
        if(model == null) {
            model = new RegisterManagerModel();
        }
        
        model.setFirstName(view.getName()[model.FIRSTNAME_POS]);
        model.setLastName(view.getName()[model.LASTNAME_POS]);
        
        String dobArray = null;
        dobArray = Integer.toString(view.getDOB()[model.DOB_YEAR_POS]) + "-" 
                + Integer.toString(view.getDOB()[model.DOB_MONTH_POS]) + "-" + 
                Integer.toString(view.getDOB()[model.DOB_DAY_POS]);
        
        model.setDOB(dobArray);
        
        model.setAddress(view.getAddress()[model.ADDRESS_POS]);        
        model.setSuburb(view.getAddress()[model.SUBURB_POS]);
        model.setState(view.getAddress()[model.STATE_POS]);
        model.setPostcode(view.getAddress()[model.POSTCODE_POS]);
       
        model.setUsername(view.getUserPass()[model.USERNAME_POS]);
        model.setPassword(view.getUserPass()[model.PASSWORD_POS]);
        model.setConfirmPassword(view.getUserPass()[model.CONFIRM_POS]);        
        
        model.setAccountType(User.AC_TYPE_MANAGER);
    }

}
